package com.cjl.demo.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MyContainer1 {
    /**volatile*/ List lists = new ArrayList();
    public void add(Object o){
        lists.add(o);
    }
    public int size(){
        return lists.size();
    }
    public static void main(String[] args) {
        MyContainer1 c = new MyContainer1();
        new Thread(()->{ //线程一
            for (int i = 0; i < 10; i++) {
                c.add(new Object());
                System.out.println("add " + i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }," t1").start();
        new Thread(()->{ //线程二
            while (true) {
                if (c.size() == 5) {
                    break;
                }
            }
            System.out.println("t2线程结束");
        }, "t2").start();
    }
}

